<?php
/**
 * 网站系统参数设置
 * @author richie
 * @package think
 */
class SystemAction extends BaseAction {

	public function index() {
		if ( isset ( $_POST ) && ! empty ( $_POST ) ) {
			$val = $_POST ['val'];
			if ( ! empty ( $_FILES ) ) {
				$upload = new UploadFile ( );
				$upload->allowExts = array (
					'txt', 
					'jpg', 
					'swf', 
					'gif', 
					'png', 
					'bmp' );
				$upload->upload ();
				if ( $upload->getErrorMsg () ) {
					message ( $upload->getErrorMsg (), array (), 1 );
				}
				$file = $upload->getUploadFileInfo ();
				if ( is_array ( $file ) && ! empty ( $file ) ) {
					foreach ( $file as $key => $f ) {
						$val [$key] = $f ['url'];
					}
				}
			}
			//更新配置值
			D ( "System" )->updateValue ( $val );
			//生成配置文件
			$this->createConfig ();
			$link [0] ['href'] = U ( "System/index" );
			$link [0] ['text'] = L ( 'back' ) . L ( 'prev' );
			message ( L ( 'saveok' ), $link );
		}
		$this->assign ( 'ur_here', L ( 'title' ) );
		$this->assign ( 'label', D ( 'System' )->labelList () );
		$this->display ();
	}

	/**
	 * 生成配置文件
	 *
	 */
	private function createConfig() {
		$path = SITE_PATH . "/data/config/";
		$val = D ( "System" )->configList ();
		$code = "<?php \n";
		$code .= "return array(\n";
		foreach ( $val as $value ) {
			$code .= "\t'{$value['var']}' => '{$value["value"]}',\n";
		}
		$code .= " );\n?>";
		//生成文件
		file_put_contents ( $path . "config.php", $code );
		//清除缓存
		rmdirr ( APP_PATH . "/runtime" );
	}
}
?>